package com.atgugu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/4/11 9:12
 */
public class Flink08_SQL_File {
    public static void main(String[] args) {
        // 流->动态表->连续查询->动态表->流
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
       
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 执行ddl语句, 建表 A, 与文件直接关联
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            " 'connector' = 'filesystem', " +
                            " 'path' = 'input/sensor.txt', " +
                            " 'format' = 'csv' " +
                            ")");
    
        Table table = tEnv.sqlQuery("select * from sensor where id='sensor_1'");
        
        tEnv.executeSql("create table a(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            " 'connector' = 'filesystem', " +
                            " 'path' = 'input/a.txt', " +
                            " 'format' = 'csv' " +
                            ")");
        
        
        // 1. 把table中的数据插入到 a表中
//        table.executeInsert("a");
        // 2. 把table中的数据插入到 a表中   sql语句: insert ... select
        tEnv.executeSql("insert into a select * from " + table);
    
    
    }
}
